package zorm

import (
	"fmt"
)

// Like 模糊查询
func (c *Conditions) Like(fieldName string, value interface{}) *Conditions {
	c.conditions = append(c.conditions, KV{K: fieldName, V: value, S: LIKE})
	return c
}

func (c *Conditions) In(fieldName string, value interface{}, bracket ...string) *Conditions {
	b := handleBracket(bracket...)
	c.conditions = append(c.conditions, KV{K: fieldName, V: value, S: IN, B: b})
	return c
}

func (c *Conditions) Between(fieldName string, value interface{}, bracket ...string) *Conditions {
	b := handleBracket(bracket...)
	c.conditions = append(c.conditions, KV{K: fieldName, V: value, S: BETWEEN, B: b})
	return c
}

func (c *Conditions) Equal(fieldName string, value interface{}, bracket ...string) *Conditions {
	b := handleBracket(bracket...)
	c.conditions = append(c.conditions, KV{K: fieldName, V: value, S: EQUAL, B: b})
	return c
}

func (c *Conditions) Del(fieldName string) *Conditions {
	for k, v := range c.conditions {
		if v.K == fieldName {
			c.conditions = append(c.conditions[:k], c.conditions[k+1:]...)
		}
	}

	return c
}

func (c *Conditions) NotNil(fieldName string, value interface{}, bracket ...string) *Conditions {
	b := handleBracket(bracket...)
	c.conditions = append(c.conditions, KV{K: fieldName, V: value, S: IS_NOT, B: b})
	return c
}

func (c *Conditions) EqualNil(fieldName string, value interface{}, bracket ...string) *Conditions {
	b := handleBracket(bracket...)
	c.conditions = append(c.conditions, KV{K: fieldName, V: value, S: IS, B: b})
	return c
}

func (c *Conditions) NotEqual(fieldName string, value interface{}, bracket ...string) *Conditions {
	b := handleBracket(bracket...)
	c.conditions = append(c.conditions, KV{K: fieldName, V: value, S: NOT_EQUAL, B: b})
	return c
}

func (c *Conditions) Less(fieldName string, value interface{}, bracket ...string) *Conditions {
	b := handleBracket(bracket...)
	c.conditions = append(c.conditions, KV{K: fieldName, V: value, S: LESS, B: b})
	return c
}

// TimeLess 时间小于minute分钟
func (c *Conditions) TimeLess(fieldName string, minute int, bracket ...string) *Conditions {
	b := handleBracket(bracket...)
	fieldName = fmt.Sprintf("TIMESTAMPDIFF(MINUTE,%s,NOW())", fieldName)
	c.conditions = append(c.conditions, KV{K: fieldName, V: minute, S: LESS, B: b})
	return c
}

// TimeGreater 时间大于minute分钟
func (c *Conditions) TimeGreater(fieldName string, minute int, bracket ...string) *Conditions {
	b := handleBracket(bracket...)
	fieldName = fmt.Sprintf("TIMESTAMPDIFF(MINUTE,%s,NOW())", fieldName)
	c.conditions = append(c.conditions, KV{K: fieldName, V: minute, S: GREATER, B: b})
	return c
}

func (c *Conditions) Greater(fieldName string, value interface{}, bracket ...string) *Conditions {
	b := handleBracket(bracket...)
	c.conditions = append(c.conditions, KV{K: fieldName, V: value, S: GREATER, B: b})
	return c
}

// GreaterEqual >=
func (c *Conditions) GreaterEqual(fieldName string, value interface{}, bracket ...string) *Conditions {
	b := handleBracket(bracket...)
	c.conditions = append(c.conditions, KV{K: fieldName, V: value, S: GREATER_EQUAL, B: b})
	return c
}

// LessEqual <=
func (c *Conditions) LessEqual(fieldName string, value interface{}, bracket ...string) *Conditions {
	b := handleBracket(bracket...)
	c.conditions = append(c.conditions, KV{K: fieldName, V: value, S: LESS_EQUAL, B: b})
	return c
}

// Limit 设置查记录条数
func (c *Conditions) Limit(value int) *Conditions {
	c.limit = fmt.Sprintf(" %s %d ", LIMIT, value)
	return c
}

// Page 分页查询(pageNo或pageSize等于，pageNo=1,pageSize=10)
func (c *Conditions) Page(pageNo int, pageSize int, offset ...int) *Conditions {
	if pageSize <= 0 || pageNo <= 0 {
		pageSize = 10
		pageNo = 1
	}
	

	startIndex := (pageNo - 1) * pageSize
	if len(offset) > 0 {
		startIndex += offset[0]
	}
	c.pages = fmt.Sprintf(" %s %d,%d", LIMIT, startIndex, pageSize)
	return c
}

// Asc 设置升序排序
func (c *Conditions) Asc(fields ...string) *Conditions {
	for _, v := range fields {
		c.store += v + ","
	}
	if len(c.store) > 1 {
		c.store = ORDER_BY + c.store[0:len(c.store)-1] + ASC
	}
	return c
}

// Desc 设置降序排序
func (c *Conditions) Desc(fields ...string) *Conditions {
	for _, v := range fields {
		c.store += v + ","
	}
	if len(c.store) > 1 {
		c.store = ORDER_BY + c.store[0:len(c.store)-1] + DESC
	}

	return c
}

// StoreRand 随机排序
func (c *Conditions) StoreRand(by ...string) *Conditions {
	if len(by) == 2 {
		c.store = fmt.Sprintf("%s %s %s,Rand()", ORDER_BY, by[0], by[1])
	} else {
		c.store = ORDER_BY + " Rand() "
	}

	return c
}

func (c *Conditions) Group(field string) *Conditions {
	c.group = fmt.Sprintf(" group by %s ", field)
	return c
}

// ClearWhere 清除where条件
func (c *Conditions) ClearWhere() *Conditions {
	c.conditions = []KV{}
	c.operation = []string{}
	return c
}

// ClearAll 清除所有条件
func (c *Conditions) ClearAll() *Conditions {
	c.conditions = []KV{}
	c.operation = []string{}
	c.limit = EMPTY
	c.pages = EMPTY
	c.store = EMPTY
	c.join = nil
	return c
}

// ClearOther 清除where之外的所有条件
func (c *Conditions) ClearOther() *Conditions {
	c.limit = EMPTY
	c.pages = EMPTY
	c.store = EMPTY
	c.join = nil
	return c
}

// And 条件连接AND运算符
func (c *Conditions) And() *Conditions {
	c.operation = append(c.operation, AND)
	return c
}

// Or 条件连接OR运算符
func (c *Conditions) Or() *Conditions {
	c.operation = append(c.operation, OR)
	return c
}

// InnerJoin 内联
func (c *Conditions) InnerJoin(tbName, leftField, rightField string) *Conditions {
	c.join = append(c.join, fmt.Sprintf(" INNER JOIN %s ON %s = %s", tbName, leftField, rightField))
	return c
}

// LeftJoin 左联
func (c *Conditions) LeftJoin(tbName, leftField, rightField string) *Conditions {
	c.join = append(c.join, fmt.Sprintf(" LEFT JOIN %s ON %s = %s", tbName, leftField, rightField))
	return c
}

// RightJoin 右联
func (c *Conditions) RightJoin(tbName, leftField, rightField string) *Conditions {
	c.join = append(c.join, fmt.Sprintf(" RIGHT JOIN %s ON %s = %s", tbName, leftField, rightField))
	return c
}
